/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.CohonController;
import Model.InitialData;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author William
 */
public class CohonPanel extends javax.swing.JPanel {

    /**
     * Creates new form CohonPanel
     */
    private InitialData initialData = Application.initialData;
    private int r;
    private CohonController cc = new CohonController();

    public CohonPanel() {
        initComponents();
        setR();
        double sol[][] = cc.execute(r);
        doTable(sol);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        cohonTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Algoritmo de Cohon [Resultados]");
        jLabel1.setToolTipText("");

        cohonTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(cohonTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addGap(5, 5, 5)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable cohonTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void setR() {
        boolean flag = true;
        while (flag) {
            try {
                r = Integer.parseInt(JOptionPane.showInputDialog(this, "Ingrese el número r:", "R", JOptionPane.QUESTION_MESSAGE));
                flag = false;
            } catch (NumberFormatException nfe) {
                flag = true;
                JOptionPane.showMessageDialog(this, "Ingrese un número.", "Error.", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void doTable(double[][] sol) {
        DefaultTableModel dtm = new DefaultTableModel();
        Object nombres[] = new Object[initialData.getNumberObjetives() * r];
        for (int i = 0; i < initialData.getNumberObjetives() * r; i++) {
            nombres[i] = "E" + ((int) (i / r) + 1);
        }
        dtm.addColumn("Nombre de Ek", nombres);
        Object valores[] = new Object[initialData.getNumberObjetives() * r];
        for (int i = 0; i < initialData.getNumberObjetives() * r; i++) {
            valores[i] = sol[i][0];
        }
        dtm.addColumn("Valor Ek", valores);

        for (int i = 1; i < initialData.getNumberVariables() + 1; i++) {
            for (int j = 0; j < initialData.getNumberObjetives() * r; j++) {
                valores[j] = sol[j][i];
            }
            dtm.addColumn("x" + i, valores);
        }
        for (int i = 0; i < initialData.getNumberObjetives() * r; i++) {
            nombres[i] = "Z" + ((int) (i / r) + 1);
        }
        dtm.addColumn("Nombres de Zn", nombres);
        for (int i = 0; i < initialData.getNumberObjetives() * r; i++) {
            valores[i] = sol[i][initialData.getNumberObjetives() + 1];
        }
        dtm.addColumn("Valor de Zn", valores);
        cohonTable.setModel(dtm);

    }
}
